package com.chenfan.mcn.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.CompanyTargetDTO;
import com.chenfan.mcn.model.dto.CompanyTargetSearchDTO;
import com.chenfan.mcn.model.vo.CompanyTargetPageVO;
import com.chenfan.mcn.service.CompanyTargetService;
import com.chenfan.privilege.common.config.SearchAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 公司目标管理
 * @author:   tangwei
 * Date:     2021/4/19 10:28
 * Description: 公司目标管理
 */
@RestController
@RequestMapping("/companyTarget/company")
public class CompanyTargetController {

    @Autowired
    private CompanyTargetService companyTargetService;

    /**
     * 新增
     * @param companyTargetDTO
     * @return
     */
    @PostMapping("/add")
    public Res<Integer> addTarget(@RequestBody CompanyTargetDTO companyTargetDTO) {
        if (StrUtil.isBlank(companyTargetDTO.getQuarterCode()) || companyTargetDTO.getTargetAmount() == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(companyTargetService.addTarget(companyTargetDTO));
    }

    /**
     * 编辑
     * @param companyTargetDTO
     * @return
     */
    @PostMapping("/update")
    public Res<Integer> updateTarget(@RequestBody CompanyTargetDTO companyTargetDTO) {
        if (companyTargetDTO.getId() == null || companyTargetDTO.getTargetAmount() == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(companyTargetService.updateTarget(companyTargetDTO));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/tail")
    public Res<CompanyTargetDTO> tailTarget(@RequestParam Long id) {
        if (id == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(companyTargetService.tailTarget(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Res<Integer> deleteTarget(@RequestParam Long id) {
        if (id == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(companyTargetService.deleteTarget(id));
    }

    /**
     * 分页列表查询
     * @param mcnPage
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Res<Page<CompanyTargetPageVO>> getPage(McnPage<CompanyTargetPageVO> mcnPage, @SearchAuthority(filterCustomer = false) CompanyTargetSearchDTO dto) {
        return Res.ok(companyTargetService.getPage(mcnPage, dto));
    }
}